package com.lions.client.model;

import com.lions.client.view.audio.AudioOutput;
import com.lions.client.view.popup.MyPopupPanel;

public class Model {

	/**
	 * Comment Default constructor makes the ModuleList
	 */
	public Model() {
		list = new ModuleList();
		minimumAlert = false;
	}

	/**
	 * Returns the actual list.
	 * 
	 * @return ModuleList
	 */
	public ModuleList getModuleList() {
		return list;
	}

	/**
	 * Adds a module to the list. Updates the session storage. Plays sound.
	 * 
	 * @param m
	 *            Module to add
	 */
	public void addModule(Module m) {
		list.add(m);
		new AudioOutput().playAddModule();
		// After each add, check to see if a minimum base is available and alert
		// user
		if ((checkMin()) && (!minimumAlert)) {
			minimumAlert = true;
			new MyPopupPanel("Minimum Alert",
					"A Minimum Base Configuration Is Available!");
			new AudioOutput().playNewBase();
		}
	}

	/**
	 * Edits a module in the list.
	 * 
	 * @param oldMod
	 *            Module to edit
	 * @param newMod
	 *            Module with new details
	 */
	public void editModule(Module oldMod, Module newMod) {
		int index = list.getModuleIndex(oldMod);
		list.remove(index);
		list.add(index, newMod);
		new AudioOutput().playEditModule();
	}

	/**
	 * Manipulates the list and html storage Removes item from list and html
	 * storage
	 * 
	 * @param x
	 *            xCoord of the module
	 * @param y
	 *            yCoord of the module
	 */
	public void removeModuleAtCoord(int x, int y) {
		list.removeModuleAtCoord(x, y);
		new AudioOutput().playRemoveModule();
	}

	/**
	 * Boolean check if there is a minimum configuration available to build.
	 * True if there is a minimum config, otherwise false. Minimum configuration
	 * consists of: 2 Connectors 1 Airlock 1 Control 1 Power 1 Storage 1
	 * Dormitory 1 Canteen 1 Sanitation
	 * 
	 * @return boolean is minimum config
	 */
	public boolean checkMin() {
		int numAIRLOCK = 0;
		int numCONTROL = 0;
		int numPOWER = 0;
		int numFOOD_WATER_STORAGE = 0;
		int numDORMITORY = 0;
		int numCANTEEN = 0;
		int numSANITATION = 0;
		int numCONNECTOR = 0;

		for (Module module : list) {
			checkModule(module);
			if (module.getCondition().toString() == "USABLE") {
				if (module.getType() == Type.AIRLOCK) {
					numAIRLOCK++;
				} else if (module.getType() == Type.CONTROL) {
					numCONTROL++;
				} else if (module.getType() == Type.POWER) {
					numPOWER++;
				} else if (module.getType() == Type.FOOD_WATER_STORAGE) {
					numFOOD_WATER_STORAGE++;
				} else if (module.getType() == Type.DORMITORY) {
					numDORMITORY++;
				} else if (module.getType() == Type.CANTEEN) {
					numCANTEEN++;
				} else if (module.getType() == Type.SANITATION) {
					numSANITATION++;
				} else if (module.getType() == Type.PLAIN) {
					numCONNECTOR++;
				}
			}
		}
		return ((numAIRLOCK > 0) && (numCONTROL > 0) && (numPOWER > 0)
				&& (numFOOD_WATER_STORAGE > 0) && (numDORMITORY > 0)
				&& (numCANTEEN > 0) && (numSANITATION) > 0 && (numCONNECTOR) > 2);
	}

	public void checkModule(Module module){
		if(module.getX() > 39 && module.getX() < 51 && module.getY() > 39){
			module.setCondition(Condition.UNREPAIRABLE);
		}
	}
	
	private boolean minimumAlert;
	private ModuleList list;
}
